import axios from "axios";
import UseUserStore from "@/api/stores/useUserStore.js";
// 创建实例并配置全局参数


const service = axios.create({
    // 使用 import.meta.env 替代 process.env
    baseURL: "http://localhost:8089",
    timeout: 1000000000000,
});

// 添加请求拦截器
service.interceptors.request.use(config => {
        const token = localStorage.getItem('token');
        if (token) {
            // config.headers.Authorization = `Bearer ${token}`; // 自动添加token
            config.headers.Authorization=window.localStorage.getItem('token')
        }
        return config;
    },
    error => Promise.reject(error)

)




service.interceptors.response.use(
    response => response.data, // 直接返回data字段
    async error => {
        if (error.response?.status === 401) {
            alert('登录过期，请重新登录');
            try {
                await router.push('/login');
            }catch (navigationError) {
                console.log('导航失败：',navigationError)
            }
        }
        return Promise.reject(error);
    }
);


export default service;
